var app = getApp();
Page({
  data: {
    pageId: 0,
    message: [],
    messageFS: [],
    guanzhuLength: "",
    fensiLength: "",
  },
  onLoad: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    //获取关注粉丝的pageID
    this.setData({
      pageId: e.pageId
    })
    //获取关注信息
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/guan',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data) {
          that.setData({
            message: res.data.message,
          })
        }
      }
    })
    //获取粉丝信息
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/fen',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data) {
          that.setData({
            messageFS: res.data.message,
          })
        }
      }
    })
  },
  //取消关注
  quxiaoguanzhu: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/nofocus',
      data: {
        openid: openid,
        aid: e.currentTarget.dataset.uid
      },
      method: "post",
      success(res) {
        console.log(res);
        if (that.data.pageId == 0) {
          for (var i = 0; i < that.data.message.length; i++) {
            if (that.data.message[i][0].uid == e.currentTarget.dataset.uid) {
              that.data.message[i][0].max = 0
            }
          }
        } else {
          for (var i = 0; i < that.data.messageFS.length; i++) {
            if (that.data.messageFS[i][0].uid == e.currentTarget.dataset.uid) {
              that.data.messageFS[i][0].max = 0
            }
          }
        }
        that.setData({
          message: that.data.message,
          messageFS: that.data.messageFS,
          guanzhuLength: that.data.guanzhuLength - 1
        })
      }
    })
  },
  //关注
  guanzhu: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/focus',
      data: {
        openid: openid,
        aid: e.currentTarget.dataset.uid
      },
      method: "post",
      success(res) {
        console.log(res);
        if (that.data.pageId == 0) {
          for (var i = 0; i < that.data.message.length; i++) {
            if (that.data.message[i][0].uid == e.currentTarget.dataset.uid) {
              that.data.message[i][0].max = 1
            }
          }
        } else {
          for (var i = 0; i < that.data.messageFS.length; i++) {
            if (that.data.messageFS[i][0].uid == e.currentTarget.dataset.uid) {
              that.data.messageFS[i][0].max = 1
            }
          }
        }
        that.setData({
          message: that.data.message,
          messageFS: that.data.messageFS,
          guanzhuLength: that.data.guanzhuLength + 1
        })
      }
    })
  },
  //返回主页
  goMyPage: function() {
    wx.reLaunch({
      url: '../myID',
    })
  },
  //跳转粉丝页面
  goFans: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    //获取粉丝信息
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/fen',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        if (res.data) {
          that.setData({
            messageFS: res.data.message
          })
        }
      }
    })
    this.setData({
      pageId: 1
    })
  },
  //跳转关注页面
  goFollow: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    //获取关注信息
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/guan',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        if (res.data) {
          that.setData({
            message: res.data.message
          })
        }
      }
    })
    this.setData({
      pageId: 0
    })
  },
  //跳转到名片
  goMingPian:function(e){
    wx.navigateTo({
      url: '../../mingpian/mingpian?uid='+e.currentTarget.dataset.uid,
    })
  }
})